from tkinter import Tk, Label, Button, Entry, Toplevel
import time

import psutil
import ctypes
import ctypes.wintypes
import win32gui
import win32gui,win32con
# 加载 Windows 用户界面相关的库
user32 = ctypes.WinDLL('user32')

# 定义相关的 Windows API 函数
FindWindowExW = user32.FindWindowExW
ShowWindow = user32.ShowWindow
EnumWindows = user32.EnumWindows
GetWindowThreadProcessId = user32.GetWindowThreadProcessId

class TimerGUI:
    def __init__(self, master):
        self.tip_window = None
        self.master = master
        master.title("计时器")
        # 设置窗口大小
        master.geometry("500x400")

        self.label = Label(master, text="输入计时时间（秒）：")
        self.label.pack()

        self.entry = Entry(master)
        self.entry.pack()

        self.start_button = Button(master, text="启动", command=self.get_task_process)
        self.start_button.pack()
        self.create_tip_button = Button(master, text="创建提示窗", command=self.create_tip_window)
        self.create_tip_button.pack()
        self.remaining_time = 0
        self.is_running = False
        self.ctrl_button = Button(master, text="控制进程窗口", command=self.control_target_process)
        self.ctrl_button.pack()

    def start_timer(self):
        if not self.is_running:
            try:
                self.remaining_time = int(self.entry.get())
                self.is_running = True
                self.start_button.config(text="停止")
                self.update_timer()
            except ValueError:
                pass

    def update_timer(self):
        if self.remaining_time > 0 and self.is_running:
            self.label.config(text=f"剩余时间：{self.remaining_time} 秒")
            time.sleep(1)
            self.remaining_time -= 1
            self.master.after(1000, self.update_timer)
        elif self.remaining_time == 0 and self.is_running:
            self.label.config(text="时间到！")
            self.start_button.config(text="启动")
            self.is_running = False

    def create_tip_window(self):
        self.tip_window = Toplevel(None)
        # self.tip_window.bg = "#000000"  # 设置背景色
        self.tip_window.cursor = "hand1"  # 隐藏鼠标
        self.tip_window.overrideredirect(True)  # 去除窗口边框
        self.tip_window.attributes("-alpha", 0.5)  # 设置半透明
        screen_width = self.master.winfo_screenwidth()
        x = (screen_width - 200) // 2
        self.tip_window.geometry(f"200x100+{x}+0")  # 顶部居中
        self.tip_window.wm_attributes("-topmost", True)  # 始终置顶  直接设置为 True

        tip_label = Label(self.tip_window, text="这是一个悬浮提示窗")
        tip_label.pack(expand=True)

    def destroy_tip_window(self):
        if self.tip_window:
            self.tip_window.destroy()
            self.tip_window = None

    # 截屏
    def get_task_process(self):
        processes = psutil.process_iter()
        # 打印进程信息
        for process in processes:
            # 获取进程名称
            name = process.name()
            # 获取进程 ID
            pid = process.pid
            # 获取进程的创建时间
            create_time = process.create_time()
            # 获取进程的状态
            status = process.status()

            print(f"进程名称: {name}, 进程 ID: {pid}, 创建时间: {create_time}, 状态: {status}")
        pass

    # 回调函数，用于枚举窗口
    def enum_windows_callback(hwnd, lParam,reserved_param=0):
        pid = ctypes.wintypes.DWORD()
        if isinstance(hwnd, int):
            GetWindowThreadProcessId(hwnd, ctypes.byref(pid))
        else:
            print(f"Invalid window handle type: {type(hwnd)}")
        if pid.value == 20392:
            # ShowWindow(hwnd, 3)  # SW_MAXIMIZE 最大化
            # ShowWindow(hwnd, 9)  # SW_RESTORE 恢复
            ShowWindow(hwnd, win32con.SW_RESTORE)  # SW_SHOW 显示
            # ShowWindow(hwnd, 6)  # SW_SHOWMINIMIZED 最小化
            # ShowWindow(hwnd, 2)  # SW_SHOWMINNOACTIVE 最小化不激活
            # ShowWindow(hwnd, 1)  # SW_SHOWNORMAL 恢复正常
            # ShowWindow(hwnd, 0)  # SW_HIDE 隐藏
            return False
        return True

    def change_other_window_title(self,window_title, new_title):
        hwnd = win32gui.FindWindow(None, window_title)
        if hwnd:
            win32gui.SetWindowText(hwnd, new_title)
            ShowWindow(hwnd, win32con.SW_RESTORE)
            win32gui.MoveWindow(hwnd, 0, 0, 1200, 850, True)
        else:
            print(f"Can't find window with title: {window_title}")

    def control_target_process(self):
        self.change_other_window_title("[叁少爷谢晓峰]", "完美世界")


root = Tk()
timer_gui = TimerGUI(root)
root.mainloop()
